import axios from 'axios';

const state = {
  cartItems: [],
  checkout: false
};

const mutations = {
  UPDATE_CART_ITEMS (state, payload) {
    state.cartItems = payload;
  },
  CHECKOUT_CART (state) {
    state.checkout = true;
  }

};

const actions = {
  getCartItems ({ commit }) {
    axios.get('/api/cart?token=D6W69PRgCoDKgHZGJmRUNA').then((response) => {
      commit('UPDATE_CART_ITEMS', response.data)
    });
  },

  addCartItem ({ commit }, cartItem) {
    axios.post('/api/cart', cartItem).then((response) => {
      commit('UPDATE_CART_ITEMS', response.data)
    });
  },
  removeCartItem ({ commit }, cartItem) {
    axios.post('/api/cart/delete', cartItem).then((response) => {
      commit('UPDATE_CART_ITEMS', response.data)
    });
  },
  removeAllCartItems ({ commit }) {
    axios.post('/api/cart/delete/all').then((response) => {
      commit('UPDATE_CART_ITEMS', response.data)
    });
  },

  checkoutCart ({ commit }, cart) {
    console.log(cart)
    axios.post('/api/cart/checkout').then((response) => {
      console.log(response)
      commit('CHECKOUT_CART');
    });
  }


};

const getters = {
  cartItems: state => state.cartItems,
  cartTotal: state => {
    return state.cartItems.reduce((acc, cartItem) => {
      return (cartItem.quantity * cartItem.price) + acc;
    }, 0).toFixed(2);
  },
  cartQuantity: state => {
    return state.cartItems.reduce((acc, cartItem) => {
      return cartItem.quantity + acc;
    }, 0);
  }


};

const cartModule = {
  state,
  mutations,
  actions,
  getters
}

export default cartModule;
